def read_lines(input_msg):
    file_name = input(input_msg)
    with open(file_name) as f:
        lines = f.readlines()
    fmt_lines = []
    for item in lines:
        fmt_lines.append(item.strip())
    return fmt_lines


def valid_customer_line(lines):
    valid_customer_flag = ['L', 'B', "A"]
    valid_customer_money = list(range(1, 100))
    valid_lines = []
    index = 0
    for item in lines:
        items = item.split(',')
        valid = True
        dict = {}
        dict['pos'] = index
        for customer in items:
            if customer[0].upper() not in valid_customer_flag or float(customer[1:]) not in valid_customer_money:
                valid = False
        if valid:
            dict['val'] = item
            valid_lines.append(dict)
        else:
            # if not valid, mark as #
            dict['val'] = '#'
            valid_lines.append(dict)
        index = index + 1
    return valid_lines


def valid_speed_line(lines):
    valid_speed = list(range(1, 1000))
    valid_lines = []
    index = 0
    for item in lines:
        dict = {}
        dict['pos'] = index
        if float(item) in valid_speed:
            dict['val'] = item
            valid_lines.append(dict)
        else:
            # if not valid,mark as #
            dict['val'] = '#'
            valid_lines.append(dict)
        index = index + 1
    return valid_lines


def valid_lines(lines, customer=None, speed=None):
    """
    verify all lines are true
    :param lines:
    :return:
    """
    valid_lines = []

    if customer:
        valid_lines = valid_customer_line(lines)

    if speed:
        valid_lines = valid_speed_line(lines)

    return valid_lines


def get_customer_pos(param):
    pos = []
    for item in param:
        if item['val'] != '#':
            pos.append(int(item['pos']) + 1)
    return pos


def display_lines():
    customer_lines = read_lines("Please input customer file:")
    speed_lines = read_lines("Please input speed file:")
    valid_customers = valid_lines(customer_lines, customer=True)
    valid_speed = valid_lines(speed_lines, speed=True)
    dict = {}
    invalid_dict = {}
    valid_reg_num = 0
    for index in range(len(valid_speed)):
        if valid_speed[index]['val'] != '#':
            valid_reg_num = valid_reg_num + 1
            dict[index] = []
        else:
            invalid_dict[index] = []

    item = 0
    indez = list(dict.keys())
    next = 0
    # round robin
    while True:
        index = (item + next) % len(indez)
        while True:
            if str(valid_customers[item]['val']).find('#') >= 0:
                item = item + 1
                next = next + 1
            else:
                break

        key = indez[index]
        dict[key].append(valid_customers[item])
        item = item + 1
        if item == len(valid_customers):
            break

    for index in range(len(valid_speed)):
        if index in invalid_dict.keys():
            print("Register {} is invalid ".format(index + 1))
        else:
            print("Register {} includes customers: {} ".format(index + 1, get_customer_pos(dict[index])))


if __name__ == '__main__':
    display_lines()
